// Recoding and cleaning dataset recieved from PSB in August 2014
// Andrew Gooch
// Yale University
// July 2018


// This code was used to de-identify and recode the data before analysis
// The final output is a cleaned dataset (data included in this replication file)
// This is commented out because it was already run on the dataset in the replication file
// The code is presented here for completeness

/*
// location of data -> be sure to replace directory location with your own directory before running!
clear
cd "C:\Users\agooch\Dropbox\Working papers\War with China\data"
use War_with_China_Experiment_Gooch


//////////////////////////////////////////////////
// PART 1: De-identify and consent variables //
//////////////////////////////////////////////////

// note: unknown variable from programming online
tab qAUD_MP2
drop qAUD_MP2

label var Respondent_Serial "Anonymous ID generated by PSB" 

// note: sample only includes those that said "yes"
rename q1 consent
label var consent "Consent to take Experiment" 


////////////////////////////////////////////////////////
// RECODE COVARIATES
////////////////////////////////////////////////////////

//Variables recoded for regression. 
//Unless noted, each is an indicator var (1=yes, 0=no)

// note: sample excludes those who are NOT likely to turnout
rename q4 Turnout_2014
label var Turnout_2014 "2014 Turnout"

// "very likely" to turnout
gen Turnout_VLikely = 0
replace Turnout_VLikely = 1 if Turnout_2014 == 1
label var Turnout_VLikely "Very Likely to Turnout in 2014"

// "somewhat likely" to turnout
gen Turnout_SLikely = 0
replace Turnout_SLikely = 1 if Turnout_2014 == 2
label var Turnout_SLikely "Somewhat Likely to Turnout in 2014"


// state of respondent
rename q5 State
label var State "State of Residence"

// region of respondent (created from state variable by PSB)
rename qREGION Region
label var Region "Region of Residence"
tab Region

// NE
gen Northeast = 0
replace Northeast = 1 if Region == 1
label var Northeast "Reside in Northeast"

// MW
gen Midwest = 0
replace Midwest = 1 if Region == 2
label var Midwest "Reside in Midwest"

// West
gen West = 0
replace West = 1 if Region == 4
label var West "Reside in West"

// Age of respondent
rename qAGE Age
label var Age "Age (5 categories)"

// Gender
rename qGENDER Gender
label var Gender "Gender of Respondent"

//Female
gen Female = 0
replace Female = 1 if Gender == 2
label var Female "Female"

// Education
rename qGOOCH_9 Education
label var Education "Education (5 categories)"

// Race 
rename qR1 Race
label var Race "Race/Ethnicity"

// African American indicator var
gen AA = 0
replace AA = 1 if Race == 2
label var AA "African American"

// Hispanic indicator var
gen Hispanic = 0
replace Hispanic = 1 if Race == 3
label var Hispanic "Hispanic"

// Open ended responses to "other" choice for race question
rename qR1_other7 Race_open_ended
label var Race_open_ended "Open Ended Responses to 'Other' in Race question"

// drop empty variables that were most likely from scripting the survey
drop QR1_O0 QR1_O1 QR1_O2 QR1_O3

// drop two race recoded variables from PSB
drop qR2 qR3





////////////////////////////////////////////////////////
// RECODE AND COMBINE PARTY IDENTIFICATION QUESTIONS
////////////////////////////////////////////////////////


// Party ID questions:
// Recode this and the leander Q into PID3 and PID7 with leaners included as partisans
// Note: three empty variables exist between the first PID question and the leaner Q.
// Then, for Regression, dummy out Dem and Ind (most likely groups to move relative to the cue (obama))

gen PID7 = .
replace PID7 = -3 if qGOOCH_1 == 1
replace PID7 = -2 if qGOOCH_1 == 2
replace PID7 = -1 if qGOOCH_2 == 1
replace PID7 = 0 if qGOOCH_2 == 2
replace PID7 = 1 if qGOOCH_2 == 3
replace PID7 = 2 if qGOOCH_1 == 4
replace PID7 = 3 if qGOOCH_1 == 5
label var PID7 "Party Identification 7 point (includes leaners as partisans)"

gen PID3 = .
replace PID3 = -1 if PID7 == -3 | PID7 == -2 | PID7 == -1
replace PID3 = 0 if PID7 == 0
replace PID3 = 1 if PID7 == 3 | PID7 == 2 | PID7 == 1
label var PID3 "Party Identification 3 point (includes leaners as partisans)"
label define PID3 -1 "Democrats" 0 "Independents" 1 "Republicans"
label values PID3 PID3 

rename QGOOC0 PID_open_ended
label var PID_open_ended "Open Ended Responses to 'Other' in PID question"

// Dems
gen Democrats = 0
replace Democrats = 1 if PID3 == -1
label var Democrats "Democrats"

// Inds
gen Independents = 0
replace Independents = 1 if PID3 == 0
label var Independents "Independents"

// drop old party vars and empty vars in-between
// note: qPARTY_CODE is PSB's recode of Party ID. It does not include leaners as partisans and simply collapses "strong" and "not strong" partisans
drop qGOOCH_1 qGOOCH_1_other6 QGOOC1 QGOOC2 QGOOC3 qGOOCH_2 qPARTY_CODE


////////////////////////////////////////////////////////
// RENAME TREATMENT VARIABLES
////////////////////////////////////////////////////////

// TREATMENT ASSIGNMENT INDICATOR VARIABLES //

// treatment assignment
rename qGOOCH_3 treatment
label var treatment "Treatment Assignment"
tab treatment

// t1 cue dummy var
gen cue_t1 = 0
replace cue_t1 = 1 if treatment == 2
label var cue_t1 "Cue Treatment"

// t2 cue dummy var
gen Inevitable_t2 = 0
replace Inevitable_t2 = 1 if treatment == 3
label var Inevitable_t2 "Inevitable Treatment"

// t3 cue dummy var
gen HumanRights_t3 = 0
replace HumanRights_t3 = 1 if treatment == 4
label var HumanRights_t3 "Human Rights Treatment"

// t bundled: cue plus any rhetoric (t2 and t3 combined)
gen cue_plus_frame_t4 = Inevitable_t2+HumanRights_t3
label var cue_plus_frame_t4 "Cue plus any frame (t2 and t3 combined)"


// OUTCOME MEASURES //
// (1) SUPPORT FOR WAR -> EACH TREATMENT ARM WAS IT'S OWN QUESTION, MUST CONCATENATE FOR REGRESSION ANALYSIS //
// (2) OBAMA FAVORABILITY -> ALL TREATMENT ARMS SAW THE SAME QUESTION, NO CONCATENATION NEEDED //

// control 
rename qGOOCH_4 t0_control
label var t0_control "Control Group"
recode t0_control (5=0) (4=.25) (3=.5) (2=.75) (1=1) (6=.)
tab t0_control

// partisan cue only (Obama)
rename qGOOCH_5 t1_cue
label var t1_cue "Cue Only Treatment Group"
recode t1_cue (5=0) (4=.25) (3=.5) (2=.75) (1=1) (6=.)

// partisan cue + justification 1 (inevitable) 
rename qGOOCH_6 t2_cue_just1
label var t2_cue_just1 "Cue + Inevitability Justification Treatment Group"
recode t2_cue_just1 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (6=.)

// partisan cue + justification 2 (human right violations) 
rename qGOOCH_7 t3_cue_just2
label var t3_cue_just2 "Cue + Human Rights Justification Treatment Group"
recode t3_cue_just2 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (6=.)

// question that combines all four randomized treatment arms into one variable
egen war_exp = rowmax(t0_control t1_cue t2_cue_just1 t3_cue_just2)
tab war_exp
label var war_exp "War with China, All Treatment Arms"

// (1) OUTCOME MEASURES FOR WAR SUPPORT
// question that combines the two rhetoric treatment arms into one variable
egen war_exp_rhetoric = rowmax(t2_cue_just1 t3_cue_just2)
tab war_exp_rhetoric
label var war_exp_rhetoric "War with China, Two Treatment Arms with Rhetoric"

// (2) OUTCOME MEASURE FOR CANDIDATE EVALUATIONS
// Obama Favorability (all respondents saw this question), recoded from zero to one excluding non-response
rename qGOOCH_8 Obama_favor
label var Obama_favor "Barack Obama Favorability"
recode Obama_favor (5=0) (4=.25) (3=.5) (2=.75) (1=1) (6=.)


////////////////////////////////////////////////////////
// INTERACTING TREATMENT AND PARTY ID
////////////////////////////////////////////////////////

// Republican will be omitted variable in regression model

// INTERACTIONS by PID
gen Dem_cue_1 = Democrats * cue_t1
label var Dem_cue_1 "Democrats x Cue Treatment"
gen Dem_Inevt_2 = Democrats * Inevitable_t2
label var Dem_Inevt_2 "Democrats x Inevitable Treatment"
gen Dem_HR_3 = Democrats * HumanRights_t3
label var Dem_HR_3 "Democrats x Human Rights Treatment"
gen Dem_Any_4 = Democrats * cue_plus_frame_t4
label var Dem_Any_4 "Democrats x Any Frame Treatment"


gen Ind_cue_1 = Independents * cue_t1
label var Ind_cue_1 "Independents x Cue Treatment"
gen Ind_Inevt_2 = Independents * Inevitable_t2
label var Ind_Inevt_2 "Independents x Inevitable Treatment"
gen Ind_HR_3 = Independents * HumanRights_t3
label var Ind_HR_3 "Independents x Human Rights Treatment"
gen Ind_Any_4 = Independents * cue_plus_frame_t4
label var Ind_Any_4 "Independents x Any Frame Treatment"


// save cleaned dataset
save War_with_China_Experiment_Gooch_cleaned, replace





*//











